﻿mainModule.controller('BaseListCtrl', ["$scope", "$rootScope", "baseEvents", "$timeout", "settings", "apiSvc", function ($scope, $rootScope, baseEvents, $timeout, settings, apiSvc) {
    "use strict";
    var events = baseEvents($scope.entityName);
    $scope.events = events;
    $scope.selectedIndex = -1;

    if(typeof($scope.sorting) === "undefined")
        $scope.sorting = { column: "Id", ascending: true };                
                
    $timeout(function () {
        $scope.paging = { currentPage: 1, pageSize: settings.data.List_PageSize, linksSize: settings.data.List_LinksSize };
        $scope.query = { PageNumber: $scope.paging.currentPage, PageSize: $scope.paging.pageSize, Count: true, SortColumn: $scope.sorting.column, SortAscending: $scope.sorting.ascending };
            
        if ($scope.autoLoadOnRender !== false) {
                findBy($scope.query);
        }
    }, 0);                

    function findBy(query) {
        apiSvc.findBy(query).then(function (response) {
            setList(response.data, query);
        });
    }

    function setList(response, query) {
        $scope.list = response.List;
        resetPagination(query, response.TotalItemCount);
    }

    function loadList() {
        $scope.query = angular.extend($scope.query, { Count: true });
        findBy($scope.query);
    }
    $scope.loadList = loadList;

    function loadListPage(pageNumber) {
        $scope.query = angular.extend($scope.query, { PageNumber: pageNumber, Count: false });
        findBy($scope.query);
    }

    function sortList(sorting) {
        $scope.sorting = sorting;
        $scope.query = angular.extend($scope.query, { SortColumn: $scope.sorting.column, SortAscending: $scope.sorting.ascending, PageNumber: 1, Count : false });
        findBy($scope.query);
    }

    function resetPagination(query, itemsCount) {
        if ($scope.paging.currentPage != query.PageNumber)
            $scope.paging.currentPage = query.PageNumber;

        if (query.Count)
            $scope.itemsCount = itemsCount;
    }

    $scope.pageChanged = function () {
        loadListPage($scope.paging.currentPage)
    }

    $scope.sortBy = function (sorting) {
        sortList(sorting);
    }

    $scope.edit = function (index) {
        var item;
        $scope.selectedIndex = index;
        item = $scope.list[index];
        $scope.$broadcast(events.editingEvent, item.Id);
    }

    $scope.remove = function (index) {
        var item = $scope.list[index];
        apiSvc.remove(item.Id).then(function (response) {
            if (response.data.Success)
                $scope.list.splice(index, 1);                
        });
        $scope.$broadcast(events.removingEvent);
    }

    $scope.add = function () {
        $scope.$broadcast(events.addingEvent, $scope.query);
    }

    $scope.onEditedEvent = function (e, item) {
        $scope.list[$scope.selectedIndex] = item;
    }

    $scope.$on(events.editedEvent, function (e, item) {
        $scope.onEditedEvent(e, item);
    });

    $scope.onAddedEvent = function (e, item) {            
        $scope.list.push(item);
    }

    $scope.$on(events.addedEvent, function (e, item) {
        $scope.onAddedEvent(e, item);
    });

    $scope.search = function (searchQuery) {
        if ($scope.searchForm.$valid) {
            $scope.query = angular.extend($scope.query, searchQuery, { Count: true });
            findBy($scope.query);
            $scope.$broadcast(events.searchingEvent);
        }
    };
}]);